package doan.chuyennganh.activity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.client.android.ZxingMainActivity;

import doan.chuyennganh.base.DoAnBaseAcitvity;
import doan.chuyennganh.model.Attendance;
import doan.chuyennganh.model.ListStudent;
import doan.chuyennganh.util.Constants;
import doan.chuyennganh.util.ListStudentDataSource;

@SuppressLint("SimpleDateFormat")
public class ScannerActivity extends DoAnBaseAcitvity implements
		OnClickListener {
	private ArrayList<String> checkList;
	private ListStudentDataSource databaseHelper;
	private Button btnScanner;
	private RadioGroup rgPeriodOption;
	private int periodNumber = 3;
	private TextView txtScannerComplete;
	private TextView txtChoosePeriodTitle;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.scanner_activity);
		btnScanner = (Button) findViewById(R.id.btn_begin_scanner);
		btnScanner.setOnClickListener(this);
		rgPeriodOption = (RadioGroup) findViewById(R.id.rg_period_number_option);
		txtScannerComplete = (TextView) findViewById(R.id.tv_scanner_complete);
		txtChoosePeriodTitle = (TextView) findViewById(R.id.tv_choose_period_title);
		rgPeriodOption
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						switch (checkedId) {
						case R.id.rd_two_period:
							pre.putInt(String.format(
									Constants.PREFERENCE_PERIOD_NUMBER,
									ListStudent.getInstance().getClassID()), 2);
							periodNumber = 2;
							break;
						case R.id.rd_three_period:
							pre.putInt(String.format(
									Constants.PREFERENCE_PERIOD_NUMBER,
									ListStudent.getInstance().getClassID()), 3);
							periodNumber = 3;
							break;
						case R.id.rd_four_period:
							pre.putInt(String.format(
									Constants.PREFERENCE_PERIOD_NUMBER,
									ListStudent.getInstance().getClassID()), 4);
							periodNumber = 4;
							break;
						case R.id.rd_five_period:
							pre.putInt(String.format(
									Constants.PREFERENCE_PERIOD_NUMBER,
									ListStudent.getInstance().getClassID()), 5);
							periodNumber = 5;
							break;
						case R.id.rd_six_period:
							pre.putInt(String.format(
									Constants.PREFERENCE_PERIOD_NUMBER,
									ListStudent.getInstance().getClassID()), 6);
							periodNumber = 6;
							break;
						default:
							break;
						}
					}
				});
		init();
	}

	public void init() {
		Attendance att = ListStudent.getInstance().getAttendanceList().getAttendanceByDate(getTodayFormatString());
		if(att != null){
			checkList = att.getAllStudentAttendanced();
		}else
		{
			checkList = new ArrayList<String>();
		}

		txtScannerComplete.setText(String.format(
				getString(R.string.txt_scanner_complete), checkList.size() + "/"
						+ ListStudent.getInstance().size()));
		txtChoosePeriodTitle.setText(String.format(getString(R.string.txt_choose_period_title), getTodayFormatString()));
		int period = pre.getInt(String.format(
				Constants.PREFERENCE_PERIOD_NUMBER, ListStudent.getInstance()
						.getClassID()), 3);
		switch (period) {
		case 2:
			((RadioButton) findViewById(R.id.rd_two_period)).setChecked(true);
			break;
		case 3:
			((RadioButton) findViewById(R.id.rd_three_period)).setChecked(true);
			break;
		case 4:
			((RadioButton) findViewById(R.id.rd_four_period)).setChecked(true);
			break;
		case 5:
			((RadioButton) findViewById(R.id.rd_five_period)).setChecked(true);
			break;
		case 6:
			((RadioButton) findViewById(R.id.rd_six_period)).setChecked(true);
			break;
		default:
			break;
		}
	}

	//fuck you, String............
	private String getTodayFormatString() {
		
		Calendar c = Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("dd_MM_yyyy");
		
		String new_col = df.format(c.getTime());

		int Hr24 = c.get(Calendar.HOUR_OF_DAY);

		if (Hr24 < 12) {
			new_col = "Sang_" + new_col;
		} else {
			new_col = "Chieu_" + new_col;
		}

		return new_col;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		if(intent == null) {
			return;
		}
		if(requestCode == Constants.SCANNER_REQUEST_CODE){
			if(resultCode == RESULT_OK){
				checkList = intent.getStringArrayListExtra(Constants.EXTRAS_CHECK_LIST);
				if(checkList != null){
					String new_col = getTodayFormatString();
					
					txtScannerComplete.setText(String.format(
							getString(R.string.txt_scanner_complete), checkList.size()
									+ "/" + ListStudent.getInstance().size()));
					databaseHelper = new ListStudentDataSource(this, ListStudent.getInstance().getClassID());
					if (databaseHelper != null) {
						databaseHelper.open();
						databaseHelper.insertColumn(new_col, periodNumber);
						databaseHelper.close();
						//databaseHelper = new ListStudentDataSource(this, ListStudent.getInstance().getClassID());
						//databaseHelper.open();
						Attendance att = ListStudent.getInstance().getAttendanceList().getAttendanceByDate(new_col);
						if(att == null){
							databaseHelper.openForReadOnly();
							ListStudent.getInstance().setAttendanceList(databaseHelper.getAllttendances());
							databaseHelper.close();
						}
						databaseHelper.open();
						for(String ms : checkList){
							databaseHelper.updateIntValue(ms, new_col, 0);
							ListStudent.getInstance().getAttendanceList().getAttendanceByDate(new_col).setAttendanceValueForStudent(ms, 0);
						}
						databaseHelper.close();
						
					} else {
						Toast.makeText(this,
								getString(R.string.txt_add_column_failed),
								Toast.LENGTH_LONG).show();
					}
				}
			}
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_begin_scanner:

			Intent scan = new Intent(this, ZxingMainActivity.class);
			scan.putStringArrayListExtra(Constants.EXTRAS_CHECK_LIST, checkList);
			getParent().startActivityForResult(scan, Constants.SCANNER_REQUEST_CODE);

			break;

		default:
			break;
		}
	}
}
